var IgnoreTagsAlgorithm = Class.create(Algorithm, {
	initialize : function(outer) {
		this.getRoot.apply(outer, []);
	},
	getRoot : function() {
		var self = this;
		// Issue 29190
		chrome.bookmarks.getChildren('1', function(results) {
					results.each(function(root) {
								if (root.title == 'delicious') {
									self.rootId = root.id;
								}
							});
					if (!self.rootId) {
						chrome.bookmarks.create({
									parentId : '1',
									title : 'delicious'
								}, function(newRoot) {
									if (newRoot)
										self.rootId = newRoot.id;
								});
					}
				});
	},

	loadCache : function() {
		var self = this;
		var w = window;
		if (!this.rootId) {
			setTimeout(function() {
						self.load()
					}, 10);
			return;
		}
		try {
			chrome.bookmarks.getChildren(this.rootId, function(bookmarks) {
				var cache = bookmarkStorage.getCache();
				var bookmarMap = {};
				for (var bm in bookmarks) {
					var hash = MD5(bm.url);
					bookmarMap[hash] = {
						url : bm.url,
						urlMD5 : hash,
						name : bm.title,
						id : bm.id
					};
				}
				for (var md5 in cache) {
					var bookmark = cache[md5];
					if (bookmark.name && bookmark.url) {
						if (!bookmarMap[bookmark.urlMD5]) {
							chrome.bookmarks.create({
										parentId : self.rootId,
										title : bookmark.name,
										url : bookmark.url
									});
							delete bookmarMap[bookmark.urlMD5];
						} else {
							console
									.log('TODO: update local bookmark from cache');
						}
					}
				}
			});
		} catch (e) {
			console.dir(e.stack || e);
		}
	},
	load : function() {
		var self = this;
		var w = window;
		if (!this.rootId) {
			setTimeout(function() {
						self.load()
					}, 10);
			return;
		}
		try {
			chrome.bookmarks.getChildren(this.rootId, function(results) {
						results.each(function(result) {
									bookmarkStorage.updateBookmark({
												urlMD5 : MD5(result.url),
												url : result.url,
												name : result.title
											}, 'local');
								});
						w.setTimeout(function() {
									self.loadCallback({
												jobId : self.loadJob.id
											});
									self.loadJob.done();

								}, 100);
					});
		} catch (e) {
			console.dir(e.stack || e);
		}
	}
});